package com.edooteach.api.servcice.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.edoomath.entity.course.Course;
import com.edoomath.entity.course.CourseMaterail;
import com.edoomath.entity.course.CourseUnit;
import com.edoomath.entity.course.MaterialType;
import com.edoomath.entity.mall.Sku;
import com.edooteach.api.dao.CourseDao;
import com.edooteach.api.servcice.CourseService;

/**
 * 
 * @author mohanlan
 *
 */
@Service("courseService")
public class CourseServiceImpl extends DataServiceImpl<Course> implements CourseService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Dao
    private CourseDao courseDao;

    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.CourseService#listAll()
     */
    @SuppressWarnings("unchecked")
    public List<Course> listAll() {
        return genericDaoSupport.searchForList("FROM Course WHERE disabled = false");
    }
    
    /**
     * 
     */
    @SuppressWarnings("unchecked")
    public List<CourseUnit> listUnit(Long id) {
        Course course = this.load(Course.class, id);
        return genericDaoSupport.searchForList("FROM CourseUnit WHERE course = :course AND disabled = false ORDER BY sorting ASC", "course", course);
    }

    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.CourseService#listMaterial()
     */
    @SuppressWarnings("unchecked")
    public List<CourseMaterail> listMaterial(Long id) {
        Course course = this.load(Course.class, id);
        return genericDaoSupport.searchForList("FROM CourseMaterail WHERE course = :course AND disabled = false ORDER BY sorting ASC", "course", course);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.CourseService#listMaterialByUnit(java.lang.Long)
     */
    @SuppressWarnings("unchecked")
    public List<CourseMaterail> listMaterialByUnit(Long id) {
        CourseUnit courseUnit = this.loadUnit(id);
        return genericDaoSupport.searchForList("FROM CourseMaterail WHERE courseUnit = :courseUnit AND disabled = false ORDER BY sorting ASC", "courseUnit", courseUnit);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.CourseService#loadByUnitType(java.lang.Long, com.edoomath.entity.course.MaterialType)
     */
    public CourseMaterail loadByUnitType(Long id, MaterialType materialType) {
        
        CourseUnit courseUnit = this.loadUnit(id);
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("courseUnit", courseUnit);
        params.put("type", materialType);
        
        return genericDaoSupport.searchForObject("FROM CourseMaterail WHERE courseUnit = :courseUnit AND type = :type AND disabled = false ORDER BY sorting ASC", params, CourseMaterail.class);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.CourseService#listMaterialByUnitType(java.lang.Long, com.edoomath.entity.course.MaterialType)
     */
    @SuppressWarnings("unchecked")
    public List<CourseMaterail> listMaterialByUnitType(Long id, MaterialType materialType) {
        
        CourseUnit courseUnit = this.loadUnit(id);
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("courseUnit", courseUnit);
        params.put("type", materialType);
        
        return genericDaoSupport.searchForList("FROM CourseMaterail WHERE courseUnit = :courseUnit AND type = :type AND disabled = false ORDER BY sorting ASC", params);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.CourseService#listMaterialByType(java.lang.Long, java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public List<CourseMaterail> listMaterialByType(Long id, MaterialType materialType) {
        Course course = this.load(Course.class, id);
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("course", course);
        params.put("type", materialType);
        
        return genericDaoSupport.searchForList("FROM CourseMaterail WHERE course = :course AND type = :type AND disabled = false ORDER BY sorting ASC", params);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.CourseService#loadUnit(java.lang.Long)
     */
    public CourseUnit loadUnit(Long unitId) {
        return genericDaoSupport.load(CourseUnit.class, unitId);
    }

    /* (non-Javadoc)
     * @see com.edooteach.api.servcice.CourseService#listBySku(com.edoomath.entity.course.Sku)
     */
    public List<Course> listBySku(Sku sku) {
        return courseDao.listBySku(sku);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.CourseService#listBySku(java.util.List)
     */
    @Override
    public Map<String, List<Course>> listBySku(List<Sku> sku) {
        
        Map<String, List<Course>> result = new HashMap<String, List<Course>>();
        
        for(Sku skuItem : sku) {
            List<Course> courses = courseDao.listBySku(skuItem);
            result.put(skuItem.getIdString(), courses);
        }
        
        return result;
    }
}
